package com.jzy.chatglmsdk18753goldn.model.GLM;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jzy.chatglmsdk18753goldn.model.ChatCompletionCommonRequest;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class GLMChatCompletionRequest extends ChatCompletionCommonRequest {

    /**
     * 模型思考  GLM-4.5加入
     */
    private Think thinking;

    /**
     * do_sample 为 true 时启用采样策略，do_sample 为 false 时采样策略 temperature、top_p 将不生效
     */
    @JsonProperty("do_sample")
    private Boolean doSample = true;

    /**
     * 用户ID
     */
    @JsonProperty("user_id")
    private String userId;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Think {
        private String type;

        public static Think enabled() {
            return Think.builder().type("enabled").build();
        }

        public static Think disabled() {
            return Think.builder().type("disabled").build();
        }
    }


    @Override
    public String toString() {
        try {
            Map<String, Object> paramsMap = new HashMap<>();
            if (null != this.getRequestId()) {
                paramsMap.put("request_id", this.getRequestId());
            }
            if (null != this.getDoSample()) {
                paramsMap.put("do_sample", this.getDoSample());
            }
            if(null != this.getThinking()){
                paramsMap.put("thinking", this.getThinking());
            }
            if(null != this.getResponseFormat()){
                paramsMap.put("response_format", this.getResponseFormat());
            }
            if(null != this.getTopP()){
                paramsMap.put("top_p", this.getTopP());
            }
            if(null != this.getUserId()){
                paramsMap.put("user_id", this.getUserId());
            }
            if (null != this.getStop() && this.getStop().size() > 0) {
                paramsMap.put("stop", this.getStop());
            }
            if (null != this.getTools() && this.getTools().size() > 0) {
                paramsMap.put("tools", this.getTools());
                paramsMap.put("tool_choice", this.getToolChoice());
            }
            if (null == this.getMessages()) {
                throw new RuntimeException("One of messages or prompt must not be empty！");
            }
            paramsMap.put("stream", this.getStream());
            paramsMap.put("messages", this.getMessages());
            paramsMap.put("model", this.getModel().getCode());
            paramsMap.put("temperature", this.getTemperature());
            paramsMap.put("max_tokens", this.getMaxTokens());
            return new ObjectMapper().writeValueAsString(paramsMap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
